package es.upm.gib.mappingAPI2;

import java.io.Serializable;

/**
 * 
 * This class is represented by three strings:
 * - Origin of the triple
 * - Destination of the triple
 * - Relation between origin and destination
 *
 */
public class Triple implements Serializable{

    //=======================================================================
    //ATTRIBUTES
    //=======================================================================

    /**
     * The name of the origin class, e.g. "http://www.ifomis.org/acgt/1.0#HumanBeing"
     */
    private String origin;

    /**
     * The name of the property linking the origin and destination classes, e.g. "http://www.ifomis.org/acgt/1.0#hasWeight"
     */
    private String relation;

    /**
     * The name of the destination class, e.g. "http://www.ifomis.org/acgt/1.0#Weight"
     */
    private String destination;


    //=======================================================================
    //CONSTRUCTORS
    //=======================================================================


    /**
     * creates a new Triple Object.
     * </p>
     * In the MappingEditor context a Triple represents a graph composed by an origin node and
     * a destination node and an arc representing a relation between the nodes.
     *
     * @param origin      the origin node in the Triple
     * @param relation    the relation arc in the Triple
     * @param destination the destination node in the Triple
     */
    public Triple(String origin, String relation, String destination) {
        this.origin = origin;
        this.relation = relation;
        this.destination = destination;
    }

    //=======================================================================
    //PUBLIC METHODS
    //=======================================================================

    /**
     * the method is used to obtain the origin instance variable of a Triple.
     *
     * @return the origin class of the Triple
     */
    public String getOrigin() {
        return origin;
    }

    /**
     * the method is used to obtain the relation instance variable of a Triple.
     *
     * @return the relation of the Triple
     */
    public String getRelation() {
        return relation;
    }

    /**
     * the method is used to obtain the destination instance variable of a Triple.
     *
     * @return the destination class of the Triple
     */
    public String getDestination() {
        return destination;
    }

    /**
     * Gives a String representation of this triple
     *
     * @return the string representation of a Triple object
     */
    public String toString() {
        return ("[" + origin + "," + relation + "," + destination + "] ");
    }

    /**
     * the method compares two Triple objects.
     * </p>
     * the method returns true only if the objects Triple have the same instance variable values
     *
     * @param triple the object to compare
     * @return the result of the comparation: true if they are equal, false otherwise. Two triples are equal when
     * their three elements (origin, relation, destination) are equal to the other triple's three elements
     */
    public boolean equals(Object triple) {
        if (triple instanceof Triple) {
            Triple instancedTriple = (Triple) triple;
            return ((origin.equals(instancedTriple.getOrigin())) &&
                    (relation.equals(instancedTriple.getRelation())) &&
                    (destination.equals(instancedTriple.getDestination())));
        }
        return false;
    }

    @Override
    public int hashCode() {
        return 31 * 31 * origin.hashCode() + 31 * relation.hashCode() + destination.hashCode();
    }
}
